package com.lcy.attendance.login.controller;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.lcy.attendance.user.mapper.UserMapper;

@Controller
public class LoginController {
	
	@Inject
	private UserMapper userMapper;
	
	@RequestMapping("/index")
	public String index() {
		return "index";
	}

	/**
	 * 用户登录
	 * @param username
	 * @param password
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping("/login")
	public String login(String username, String password, Model model, HttpServletRequest request) {
		HttpSession session = request.getSession();
		
		/* 检查用户密码 测试代码 只是匹配密码 */
		if(username.equals("")) {
			model.addAttribute("message", "用户名不能为空");
			return "index";
		}
		if(password.equals("")) {
			model.addAttribute("message", "密码不能为空");
			return "index";
		}
		String passwordFromDB = userMapper.getUserPasswordByUsername(username);
		if(passwordFromDB == null || passwordFromDB.equals("")) {
			model.addAttribute("message", "该用户不存在");
			return "index";
		}
		if(password.equals(passwordFromDB)) {
			session.setAttribute("username", username);
			return "main";
		} else {
			model.addAttribute("message", "密码错误");
			return "index";
		}
	}
}
